<template>
	<view ref="person">
		<view v-if="staffList != ''">
			<uni-card :is-shadow="false" v-for="(item,index) in staffList" :key="index">
				<p class="grid-integral">员工姓名：{{ item.name }}</p>
				<p class="grid-integral">手机号码：{{ item.mobile }}</p>
				<p class="grid-integral">实名认证：
					<template v-for="(dictItem, index) in dicts.ec_certify_staff_status">
						<template v-if="item.isCertify == dictItem.value">
							<uni-tag :text="dictItem.text" :type="dictItem.type" />
						</template>
					</template>
				</p>
				<p class="grid-hidden">员工职位：
					<span v-if="!!item.staffPost">{{ item.staffPost }}</span>
					<span v-else>--</span>
				</p>
				<p class="grid-integral">员工部门：{{ item.deptName }}</p>
				<p class="grid-integral">权限角色：
					<span
						v-if="$store.getters.companyInfo.adminId == item.id && $store.getters.companyInfo.legalPersonId == item.id">
						法定代表人,企业管理员,{{ item.roleName.replace("普通员工", "") }}
					</span>
			  <span v-else-if="$store.getters.companyInfo.legalPersonId == item.id">
						法定代表人,{{ item.roleName.replace("普通员工", "") }}
					</span>
					<span v-else-if="$store.getters.companyInfo.adminId == item.id">
						企业管理员,{{ item.roleName.replace("普通员工", "") }}
					</span>
					<span v-else>{{ item.roleName }}</span>
				</p>
				<p class="grid-integral">账号状态：
					<template v-for="(dictItem, index) in dicts.ec_sys_status">
						<template v-if="item.status == dictItem.value">
							<uni-tag :text="dictItem.text" :type="dictItem.type" />
						</template>
					</template>
				</p>	
				<view slot="actions" class="card-actions">
				  <view></view>
				  <view class="flex_between">
				  <view class="card-actions-item" @click="handleUpdate(item)">
				    <text class="card-actions-item-text warningColor">修改</text>
				  </view>
				  <template v-if="$store.getters.companyInfo.adminId != item.id && $store.getters.companyInfo.legalPersonId != item.id">
					<view class="card-actions-item" @click="handleEnableStaff(item)" v-if="item.status == 0">
					  <text class="card-actions-item-text themeColor">启用</text>
					</view> 
					 
				  <view class="card-actions-item" @click="handleDisableStaff(item)" v-if="item.status == 1">
				    <text class="card-actions-item-text errorColor">禁用</text>
				  </view>
				  
				  <view class="card-actions-item" @click="handledelete(item,index)" style="margin-right: 0;">
				    <text class="card-actions-item-text errorColor">移除</text>
				  </view>
				  </template>
				</view>
				</view>
			</uni-card>
			<!-- 加载更多 -->
			<uni-load-more color="#00a660" :status="loadStatus" />
		</view>
		<!-- 暂无数据 -->
		<luanqingEmpty :show="true" text="没有更多数据啦" v-else />
	</view>
</template>

<script>
	import {getDictsByNameForUni} from "@/utils/dicts";
	import {countApplyIngNum} from "@/api/ec/common/staffApply";
	import {listCompanyRole} from "@/api/ec/console/org/companyRole";
	import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
	import {addDept,delDept,treeselect,updateDept} from "@/api/ec/console/org/dept";
	import {addStaff,delStaff,getStaff,listStaffPager,updateStaff,updateStaffStatus} from "@/api/ec/console/org/staff";
	export default {
		name: "personnelManagement",
		components: {luanqingEmpty},
		props: {
			// 查询参数
			findApi: {
				type: String,
				default: null
			},
		},
		data() {
			return {
				// 数据字典
				dicts: {
					ec_certify_staff_status: [],
					ec_sys_status: [],
				},
				total: 0, //总条数
				loadStatus: 'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					isCertify: null,
					status: null,
					params: {
						findApi: undefined
					}
				},
				staffList: [], //人员信息
			}
		},
		created() {
			this.getList();
			this.getDicts(); //字典查询
			// 加载页面数据
			uni.$on('refreshData', () => {
				this.getList(true);
			})
		},
		methods: {
			/** 上拉刷新回调 */
			loadMore() {
				if (this.loadStatus != 'noMore' && this.total > this.staffList.length) {
					this.loadStatus = 'loading';
					// 延迟500毫秒显示loading效果
					setTimeout(() => {
						// 执行查询的方法
						this.getList(false);
					}, 500)
				} else {
					// 停止加载
					this.loadStatus = 'noMore'
				}
			},
			/**加载数据字典*/
			getDicts() {
				// 签署流量包效期 
				this.dicts.ec_certify_staff_status = getDictsByNameForUni("ec_certify_staff_status");
				this.dicts.ec_sys_status = getDictsByNameForUni("ec_sys_status") //账号状态
			},
			/**
			 * 查询列表
			 * @param reload true 重新加载数据； false 加载更多
			 */
			getList(reload) {
				if (reload) {
					this.queryParams.pageNum = 1;
					this.loadStatus = 'more'
          this.$modal.loading();
				}
				if (this.findApi != null) {
					this.queryParams.params.findApi = this.findApi
					this.staffList = []
				}
				listStaffPager(this.queryParams).then(response => {
					if (reload) {
						uni.hideLoading()
			  }
					this.total = response.total;
					if (response.total > 0) {
						const dataRows = response.rows;
						this.staffList = reload ? dataRows : this.staffList.concat(dataRows);
					} else {
						this.staffList = [];
					}
					// 判断数据是否加载完全
					if (response.total <= this.staffList.length) {
						this.loadStatus = 'noMore'
					}
					// 页数+1
					this.queryParams.pageNum++
				}).catch(() => {
					if (reload) {
						uni.hideLoading()
					}
				});
			},
			/** 移除成员 */
			handledelete(item, index) {
				let that = this
				uni.showModal({
					title: '系统提示',
					content: '确定移除成员？',
					confirmColor: '#00a660',
					cancelText: '取消',
					confirmText: '确定删除',
					success: function(res) {
						if (res.confirm) {
							let id = item.id
							delStaff(id);
							that.getList(true)
							that.$modal.msgSuccess("移除成功");
						}
					}
				})
			},
			/** 修改 */
			handleUpdate(item) {
				getStaff(item.id).then((response) => {
					let form = response.data
					uni.navigateTo({
						url: '/pages/mine/work/organizationalManagement/personnelManage/newMember?form=' +
							JSON.stringify(form)
					})
				})
			},
			/** 启用员工 */
			handleEnableStaff(row) {
				let data = {
					staffId: row.id,
					status: 1
				};
        this.$modal.loading();
				updateStaffStatus(data).then(() => {
					uni.hideLoading()
					this.getList(true);
					this.$modal.msgSuccess("成员启用成功");
				}).catch(() => {});
			},
			/** 禁用员工 */
			handleDisableStaff(row) {
				var that = this
				let data = {
					staffId: row.id,
					status: 0
				};
				uni.showModal({
					title: '系统提示',
					confirmColor: '#00a660',
					content: '是否确认禁用该成员',
					success: function(res) {
						if (res.confirm) {
							updateStaffStatus(data)
							that.getList(true)
							that.$modal.msgSuccess("成员禁用成功");
						} else if (res.cancel) {}
					},
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	::v-deep .uni-card .uni-card__content{
		padding-bottom: 0 !important;
	}
	.guanliIcon {
		margin-left: 5rpx;
		vertical-align: middle;
		margin-bottom: 2rpx;
	}

	.certiyImg {
		width: 26rpx;
		height: 26rpx;
		margin-left: 3rpx;
		display: inline-block;
		vertical-align: middle;
		margin-bottom: 8rpx;
	}

	// ::v-deep .dropdown-item__content{top:590rpx !important;}
	::v-deep .dropdown-item__content .list {
		left: 20rpx
	}
</style>
